package com.qy.controller.app;

import com.qy.base.core.Constants;
import com.qy.base.core.Result;
import com.qy.base.core.ResultGenerator;
import com.qy.base.utils.DateUtil;
import com.qy.base.utils.RongYunSMS;
import com.qy.base.utils.kuaizuyunSMS.KuaiZuYunSMS;
import com.qy.model.Sms;
import com.qy.service.SmsService;
import com.qy.base.core.PageBean;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by sxd on 2018/06/06.
 */
@RestController
@RequestMapping("app/sms")
public class SmsController {
    @Resource
    private SmsService smsService;

    @PostMapping("/add")
    public Result add(@RequestBody Sms sms) {
        smsService.save(sms);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        smsService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Sms sms) {
        smsService.update(sms);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Sms sms = smsService.findById(id);
        return ResultGenerator.successResult(sms);
    }

    @GetMapping("/list")
    public Result list(PageBean<Sms> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Sms> list = smsService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }


    /**
     * 发送短信
     */
    @PostMapping("/sendSMS")
    public Result sendSMS(@RequestBody Sms sms) throws Exception {
        String phone = sms.getPhone();
        if (phone == null || "".equals(phone)) {
            return ResultGenerator.errResult(Constants.CODE_ERR_PHONE_NUMBER);
        }
        Random random = new Random();
        String content = String.valueOf(random.nextInt(9999) % (9999 - 1000 + 1) + 1000);
        Boolean result = KuaiZuYunSMS.sendSMS(phone, content, 4);
        if (result) {
            sms.setPhone(sms.getPhone());
            sms.setCode(content);
            sms.setAdd_time(DateUtil.getNowTimestamp());
            smsService.save(sms);
            return ResultGenerator.successResult();
        } else {
            return ResultGenerator.errResult(Constants.FAIL);
        }
    }

    /**
     * 检查短信
     */
    @GetMapping("/checkSMS")
    public Result checkSMS(Sms sms) throws Exception {
        // 检查参数
        if (sms.getPhone().length() <= 0 || sms.getCode().length() <= 0) {
            return ResultGenerator.errResult(Constants.CODE_ERR_PARAM);
        }
        Condition condition = new Condition(Sms.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andCondition("phone = " + sms.getPhone());
        criteria.andCondition("code = " + sms.getCode());
        List<Sms> smsList = smsService.findByCondition(condition);
        Boolean isFalse = false;
        for (Sms aSmsList : smsList) {
            if (new Date().getTime() - Long.parseLong(aSmsList.getAdd_time()) < 1000 * 1800) {
                isFalse = true;
            }
        }
        if (isFalse) {
            return ResultGenerator.successResult();
        } else {
            return ResultGenerator.errResult(Constants.CODE_ERR_SMS_INVALID);
        }
    }
}
